using System;
using System.Xml.Serialization;

namespace SharpMud.Server.App.Hosting
{
	/// <summary>
	/// This class is used to track configuration information for an ApplicationHost.
	/// This class is designed to be easilly Xml Serializable.
	/// </summary>
	[XmlRoot("ApplicationHostConfiguration")]
	[XmlInclude(typeof(SharpMud.DynamicTyping.AssembliedTypeLocation))]
	public class ApplicationHostConfiguration
	{
		/// <summary>
		/// The default public constructor for the ApplicationHostConfiguration class.
		/// </summary>
		public ApplicationHostConfiguration()
		{
		}

		private SharpMud.DynamicTyping.TypeLocation _ServerApp;
		private string								_RemotingPoint;
        private bool                                _CatchUnhandledExceptions;

		/// <summary>
		/// This is a pointer to the assembly and type of the MudLib (server app)
		/// </summary>
		[XmlElement("ServerAppLocation")]
		public SharpMud.DynamicTyping.AssembliedTypeLocation ServerAppLocation
		{
			get
			{
				return (SharpMud.DynamicTyping.AssembliedTypeLocation)_ServerApp;
			}
			set
			{
				_ServerApp = value;
			}
		}

		[XmlElement("RemotingPoint")]
		public string RemotingPoint
		{
			get
			{
				return _RemotingPoint;
			}
			set
			{
				_RemotingPoint = value;
			}
		}

        [XmlElement("CatchUnhandledExceptions")]
        public bool CatchUnhandledExceptions
        {
            get
            {
                return _CatchUnhandledExceptions;
            }
            set
            {
                _CatchUnhandledExceptions = value;
            }
        }

		[XmlIgnore()]
		public System.Uri RemotingPointUri
		{
			get
			{
				if(RemotingPoint==null)
					return null;
				if(RemotingPoint==String.Empty)
					return null;
                return new System.Uri(RemotingPoint);
			}
			set
			{
				RemotingPoint = value.ToString();
			}
        }

        public System.Xml.XmlDocument ToXmlDocument()
        {
            return ApplicationHostConfiguration.ToXmlDocument(this);
        }

        #region Static Stuff
        public static System.Xml.XmlDocument ToXmlDocument(ApplicationHostConfiguration appHostConfiguration)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Xml.Serialization.XmlSerializer xs = new XmlSerializer(appHostConfiguration.GetType());
            xs.Serialize(ms,appHostConfiguration);
            ms.Position=0;

            System.IO.StreamReader sr = new System.IO.StreamReader(ms);
            
            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
            xd.InnerXml = sr.ReadToEnd();

            return xd;
        }
        public static ApplicationHostConfiguration FromXmlDocument(System.Xml.XmlDocument xmlDocument)
        {
            System.IO.StringReader sr = new System.IO.StringReader(xmlDocument.InnerXml);
            System.Xml.Serialization.XmlSerializer xs = new XmlSerializer(typeof(ApplicationHostConfiguration));
            return (ApplicationHostConfiguration)xs.Deserialize(sr);
        }
        #region Operator Overloads
        public static explicit operator System.Xml.XmlDocument(ApplicationHostConfiguration config)
        {
            return ApplicationHostConfiguration.ToXmlDocument(config);
        }
        public static explicit operator ApplicationHostConfiguration(System.Xml.XmlDocument document)
        {
            return ApplicationHostConfiguration.FromXmlDocument(document);
        }
        #endregion
        #endregion
    }
}